package com.zshan.clinic.mini.controller;

import com.zshan.clinic.admin.entity.vo.CmUserPatientVo;
import com.zshan.clinic.admin.service.customer.CmUserPatientService;
import com.zshan.clinic.common.response.RestResp;
import com.zshan.clinic.database.model.CmUserPatient;
import com.zshan.clinic.mini.entity.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 就诊人管理
 */
@RestController
@RequestMapping("/cmUserPatient")
public class CmUserPatientController extends AbstractController {

    @Autowired
    private CmUserPatientService cmUserPatientService;

    /**
     * 查询我的就诊人列表
     * @param isDefault 是否默认（用于预约页面默认就诊人）
     * @return
     */
    @GetMapping("/getPatientList")
    public RestResp<List<CmUserPatientVo>> getPatientList(@RequestParam(name = "isDefault",required = false) String isDefault) {
        UserInfo userInfo = getUserInfo();
        List<CmUserPatientVo> patientList = cmUserPatientService.getPatientList(userInfo.getClinicId(),userInfo.getCmUserId(),isDefault);
        return RestResp.SUCCESS(patientList);
    }

    /**
     * 添加就诊人
     * @param cmUserPatient
     * @return
     */
    @PostMapping("/addPatient")
    public RestResp<Void> addPatient(@RequestBody CmUserPatient cmUserPatient) {
        UserInfo userInfo = getUserInfo();
        cmUserPatient.setCmUserId(userInfo.getCmUserId());
        cmUserPatient.setCreateUserId(userInfo.getCmUserId());
        cmUserPatient.setCreateBy(userInfo.getNickName());
        cmUserPatient.setClinicId(userInfo.getClinicId());
        cmUserPatientService.addCmUserPatient(cmUserPatient);
        return RestResp.SUCCESS();
    }


    /**
     * 修改就诊人
     * @param cmUserPatient
     * @return
     */
    @PostMapping("/updatePatient")
    public RestResp<Void> updatePatient(@RequestBody CmUserPatient cmUserPatient) {
        UserInfo userInfo = getUserInfo();
        cmUserPatient.setCmUserId(userInfo.getCmUserId());
        cmUserPatient.setUpdateUserId(userInfo.getCmUserId());
        cmUserPatient.setUpdateBy(userInfo.getNickName());
        cmUserPatient.setClinicId(userInfo.getClinicId());
        cmUserPatientService.updateCmUserPatient(cmUserPatient);
        return RestResp.SUCCESS();
    }

    /**
     * 删除就诊人
     * @param cmUserPatient
     * @return
     */
    @PostMapping("/deletePatient")
    public RestResp<Void> deletePatient(@RequestBody CmUserPatient cmUserPatient) {
        UserInfo userInfo = getUserInfo();
        cmUserPatient.setCmUserId(userInfo.getCmUserId());
        cmUserPatient.setUpdateUserId(userInfo.getCmUserId());
        cmUserPatient.setUpdateBy(userInfo.getNickName());
        cmUserPatient.setClinicId(userInfo.getClinicId());
        cmUserPatientService.deleteCmUserPatient(cmUserPatient);
        return RestResp.SUCCESS();
    }

    /**
     * 设置默认就诊人
     * @param cmUserPatient
     * @return
     */
    @PostMapping("/setDefaultPatient")
    public RestResp<Void> setDefaultPatient(@RequestBody CmUserPatient cmUserPatient) {
        UserInfo userInfo = getUserInfo();
        cmUserPatient.setCmUserId(userInfo.getCmUserId());
        cmUserPatient.setUpdateUserId(userInfo.getCmUserId());
        cmUserPatient.setUpdateBy(userInfo.getNickName());
        cmUserPatient.setClinicId(userInfo.getClinicId());
        cmUserPatientService.setDefaultPatient(cmUserPatient);
        return RestResp.SUCCESS();
    }
}

